package controller

import (
	jsoniter "github.com/json-iterator/go"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/hotelOrder/model"
	"strconv"
)

type HotelController struct {
	Ctx iris.Context
	HotelOrder *model.HotelOrder
}

func (hc *HotelController)PostPlaceanorder() mvc.Result {

	hc.HotelOrder = new(model.HotelOrder)

	hotelOrderStr := hc.Ctx.FormValue("hotelOrder")
	var hotelOrder model.HotelOrder
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(hotelOrderStr), &hotelOrder)

	residentName := hc.Ctx.FormValue("residentName")

	//dateListStr := hc.Ctx.FormValue("dateList")
	//var dateList []string
	//jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(dateListStr), &dateList)


	res, err := hc.HotelOrder.PlaceAnOrder(&hotelOrder, residentName)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (hc *HotelController)PostCheckin() mvc.Result {

	hc.HotelOrder = new(model.HotelOrder)

	checkInInformationIdStr := hc.Ctx.FormValue("checkInInformationId")
	checkInInformationId, err := strconv.ParseInt(checkInInformationIdStr, 10 ,64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	roomIdStr := hc.Ctx.FormValue("roomId")
	roomId, err := strconv.ParseInt(roomIdStr, 10 ,64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := hc.HotelOrder.CheckIn(checkInInformationId, roomId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (hc *HotelController)PostQueryroominfo() mvc.Result {

	hc.HotelOrder = new(model.HotelOrder)

	pageNoStr := hc.Ctx.URLParam("pageNo")
	pageNo, _ := strconv.Atoi(pageNoStr)
	pageSizeStr := hc.Ctx.URLParam("pageSize")
	pageSize, _ := strconv.Atoi(pageSizeStr)
	res, total, err := hc.HotelOrder.QueryRoomInfo(pageNo, pageSize)
	if err != nil{
		hc.Ctx.Header("status", "500")
		return mvc.Response{
			Code: 500,
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	hc.Ctx.Header("status", "200")
	return mvc.Response{
		Code: 200,
		Object : map[string]interface{}{"message": "成功", "data": map[string]interface{}{"rows":res, "total":total}, "code":"200",},
	}
}